import axios from 'axios';

let env = process.env.NODE_ENV;

let util = {};
util.ajax = {};
util.vars = {};

const appId_test = "wxb93cabf634c1bd02";   // 测试环境公众号appid
const appId_online = "wxb99ce1f5ae2420f2"; // 线上公众号appid
let domain = 'http://'+window.location.hostname+'/';
util.vars.htmlUrl=domain;
util.vars.lastIndex=[];
util.vars.ajaxUrl=domain + 'api/';

if (domain.indexOf("www.davfang.com") > -1) {
  util.vars.wxLink = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId_online + '&response_type=code&scope=snsapi_userinfo&state=1&redirect_uri=' + encodeURIComponent(util.vars.htmlUrl + 'auth.html') + '#wechat_redirect'
}else {
  domain = 'http://c.davfang.com/';
  util.vars.wxLink = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId_test + '&response_type=code&scope=snsapi_userinfo&state=1&redirect_uri=' + encodeURIComponent(util.vars.htmlUrl + 'auth.html') + '#wechat_redirect'
}

axios.interceptors.response.use(function (response) {
  // 数据请求成功
  if (response.data.code == 1) {
    return response.data;
  } else if (response.data.code == 0) {
    return Promise.reject(response);
  } else {
    return response.data;
  }

}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);

});

axios.defaults.timeout = 30000;

util.ajax.get = function (url, baseURL) {


  return new Promise((resolve, reject) => {

    axios({
      method: 'get',
      url,
      baseURL: baseURL || util.vars.ajaxUrl,
    }).then(res => {
      resolve(res)
    })
  });

};
util.ajax.post = function (url, params, baseURL) {

  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url,
      data: params,
      baseURL: baseURL || util.vars.ajaxUrl,

    }).then(res => {
      resolve(res)
    })
  });
};

util.GetParameterUrl = function (name) {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let urlLink = window.location.href;
  let hrefLink = urlLink.indexOf('?') > -1 ? urlLink.indexOf('#') > -1 ? urlLink.split('?')[1].split('#')[0] : urlLink.split('?')[1] : '';
  let r = hrefLink.match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};


util.formatDate = function (date) {
  let time = new Date();
  if (typeof(date) === 'number') {
    time = new Date(date);
  } else {
    let dateFor = date.toString().replace(/\-/g, "/");
    time = new Date(dateFor);
  }

  let nowD = new Date();//当前时间

  //相隔的天数
  let diffDay = Math.round(Math.abs((nowD.getTime() - time.getTime())) / (1000 * 60 * 60 * 24));
  if (diffDay >= 3 && diffDay < 4) {
    return '三天前';
  }
  else if (diffDay >= 2 && diffDay < 3) {
    return '两天前';
  }
  else if (diffDay >= 1 && diffDay < 2) {
    return '昨天';
  }
  else if (diffDay >= 0 && diffDay < 1) {
    let diffHour = Math.round(Math.abs((nowD.getTime() - time.getTime())) / (1000 * 60 * 60 ));
    //当天，需要判断小时
    if (diffHour >= 3) {
      return '今天';
    }
    else if (diffHour >= 2 && diffHour < 3) {
      return '2小时前';
    } else if (diffHour >= 1 && diffHour < 2) {
      return '1小时前';
    } else {
      //当前小时，判断分钟
      let diffMin = Math.round(Math.abs((nowD.getTime() - time.getTime())) / (1000 * 60));
      if (diffMin >= 10) {
        return diffMin + '分钟前';
      } else {
        return '刚刚';
      }
    }
  } else {
    return `${time.getFullYear()}-${('0' + (time.getMonth() + 1)).slice(-2)}-${('0' + time.getDate()).slice(-2) }`;
  }
};
util.jsonStringify = function (arg) {
  let qsArr = [];
  for (let k in arg) {
    let v = arg[k];
    qsArr.push({
      name: k,
      value: ("" + v).toString()
    })
  }
  for (let i = 0; i < qsArr.length; i++) {
    qsArr[i] = [qsArr[i].name, qsArr[i].value].join('=')
  }
  return qsArr.join('&');
};

export default util;
